package com.sneakxy.mybatis.commons.repository.query.executor;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.binding.MapperMethod.ParamMap;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.scripting.xmltags.DynamicSqlSource;
import org.apache.ibatis.scripting.xmltags.TextSqlNode;

import com.sneakxy.mybatis.commons.orm.MybatisEntityManager;
import com.sneakxy.mybatis.commons.orm.generation.GenerationManager;
import com.sneakxy.mybatis.commons.orm.mapping.MybatisColumn;
import com.sneakxy.mybatis.commons.repository.query.parser.MybatisPartTree;

/**
 * @author 潜行的青衣
 */
public class DeleteRepositoryExecutor extends AbstractRepositoryExecutor<Long> {

	/**
	 * @param entityManager
	 * @param loader
	 * @param method
	 * @param type
	 */
	public DeleteRepositoryExecutor(MybatisEntityManager entityManager, Class<?> loader, String method,
			Class<?>... type) {
		super(entityManager, loader, method, type);
	}

	/**
	 * @param entityManager
	 * @param method
	 */
	public DeleteRepositoryExecutor(MybatisEntityManager entityManager, Method method) {
		super(entityManager, method);
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybatis.commons.repository.query.executor.RepositoryExecutor#execute(java.lang.Object[])
	 */
	@Override
	public Long execute(Object... parameters) {
		Object parameterObject = this.createParameterObject(parameters, false);
		return Long.valueOf(this.getEntityManager().getSqlSessionTemplate().delete(getStatement(), parameterObject));
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybatis.commons.repository.query.executor.AbstractRepositoryExecutor#onBuildStatment()
	 */
	@Override
	protected void onBuildStatment() {
		GenerationManager generationManager = this.getEntityManager().getGenerationManager();
		StringBuilder sqlBuilder = new StringBuilder();
		StringBuilder whereBuilder = new StringBuilder();
		List<MybatisColumn> columns = this.getEntityManager().getEntityColumns(StringUtils.join(MybatisPartTree.GENERIC_NAME_PREFIX, "1"));
		Iterator<MybatisColumn> it = columns.iterator();
		MybatisColumn column = null;
		whereBuilder.append("WHERE ");
		while(it.hasNext()) {
			column = it.next();
			if(column.isPrimary()) {
				whereBuilder.append(generationManager.formatColumnName(column.getColumn())).append("=");
				whereBuilder.append("#{").append(this.getParameterPath(column)).append("} AND ");
			}
		}
		whereBuilder.delete(whereBuilder.length() - 4, whereBuilder.length());
		sqlBuilder.append("DELETE FROM ${").append(MybatisPartTree.GENERIC_NAME_PREFIX).append("2} ").append(whereBuilder.toString());
		String id = this.getStatement();
		String sql = sqlBuilder.toString();
		SqlCommandType sqlCommandType  = SqlCommandType.DELETE;
		SqlSource sqlSource = new DynamicSqlSource(this.getEntityManager().getConfiguration(), new TextSqlNode(sql));
		this.getEntityManager().addMappedStatement(id, sqlSource, sqlCommandType, ParamMap.class, Long.class);
	}

}
